import pymysql


def delete(table_name):
    con = pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()

    min = cur.execute("SELECT min(id) FROM %s " % table_name)
    min = int(cur.fetchone()[0])
    max = cur.execute("SELECT max(id) FROM %s " % table_name)
    max = int(cur.fetchone()[0]) - 1000

    sql2 = ("delete FROM %s " % table_name + "WHERE id <= %s " % max + " and id >= %s" % min)
    cur.execute(sql2)
    con.commit()
    cur.close()
    con.close()


delete("DHT11")
delete("di_s")
delete("do_s")
delete("err_msg")
delete("suc_msg")
delete("Injection_valve")
delete("PH_controller")
